﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BranchManger.aspx.cs" Inherits="TaoSH.View.BranchManger" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link rel="stylesheet" href="../../CSS/bootstrap/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../JqueryPlug/zTreeStyle/zTreeStyle.css" />
    <link rel="stylesheet" type="text/css" href="../../JqueryPlug/smartMenu/css/smartMenu.css" />
    <script src="../../Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../JqueryPlug/zTreeStyle/jquery-ztree-2.5.min.js"></script>
    <script type="text/javascript" src="../../JqueryPlug/smartMenu/js/jqueryMenu.js"></script>
    <script type="text/javascript">
        var zTree1, rMenu, setting;
        function getMenu() {
            setting = {
                isSimpleData: true,
                treeNodeKey: "id",
                treeNodeParentKey: "pId",
                showLine: false,

                callback: {
                    rightClick: zTreeOnRightClick,
                    click: zTreeOnClick

                },
                root: {
                    isRoot: true,
                    nodes: []
                }
            };
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "GetBranch" },
                beforeSend: function (XMLHttpRequest) {
                    MyDialog.Mytips();
                },
                success: function (msg) {
                    zNodes = eval(msg);
                    zTree1 = $("#tree").zTree(setting, zNodes);
                    zTree1.expandAll(true);

                },
                complete: function (XMLHttpRequest, textStatus) {
                    MyDialog.CloseMytips();
                },
                error: function () {
                    alert('error');
                }
            });
        }
        function showRMenu(type, x, y) {
            $("#smartMenu_image2").show();
            $("#smartMenu_image2").css({ "top": y + "px", "left": x + "px", "display": "block" });
        }
        function hideRMenu() {
            $("#smartMenu_image2").hide();
        }

        function zTreeOnRightClick(event, treeId, treeNode) {
            if (!treeNode) {
                zTree1.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else if (treeNode && !treeNode.noR) {
                zTree1.selectNode(treeNode);
                showRMenu("bran", event.clientX, event.clientY);
            }

            //筛选数据
            $(".ftitle").text(treeNode.name + '人员');
            $("#MulTab tbody tr").each(function () {
                if ($.trim($(this).find('td').eq(2).text()) != treeNode.name && treeNode.id != '0') {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            })
        }
        function zTreeOnClick(event, treeId, treeNode) {
            //筛选数据
            $(".ftitle").text(treeNode.name + '人员');
            $("#MulTab tbody tr").each(function () {
                if ($.trim($(this).find('td').eq(2).text()) != treeNode.name && treeNode.id != '0') {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            })
        }
        function ManageBranch(action) {

            var node = zTree1.getSelectedNode();
            if (action == 'add') {
                var pageurl = "MangerBranch.aspx?Action=add&Pid=" + node.id;
                MyDialog.viewform(pageurl, '增加部门', 550, 400);
            }
            if (action == 'update') {
                var pageurl = "MangerBranch.aspx?Action=update&Pid=" + node.id;
                MyDialog.viewform(pageurl, '修改部门', 550, 400);
            }
            if (action == 'del') {
                if (node) {
                    if (node.nodes && node.nodes.length > 0) {
                        MyDialog.warning('要删除的节点是父节点，请先删除子节点！');
                    } else {
                        MyDialog.confirm('删除部门会删除部门下得所有用户,你确定要删除部门吗？', function () {
                            delBranch(node);
                        });
                    }
                }
                hideRMenu();
            }
            if (action == 'addUser') {
                AddUser();
            }
        }

        //删除部门
        function delBranch(node) {
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "delBranch", "delCode": node.id },
                beforeSend: function (XMLHttpRequest) {
                    MyDialog.Mytips();
                },
                success: function (msg) {
                    if (true) {
                        zTree1.removeNode(node);
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    MyDialog.CloseMytips();
                    MyDialog.success('删除成功')
                },
                error: function () {
                }
            });
        }
        //添加用户
        function AddUser() {
            var node = zTree1.getSelectedNode();
            var pid = node == null ? "1" : node.id;
            var pageurl = "MangerUser.aspx?Action=add&Pid=" + pid;
            MyDialog.viewform(pageurl, '添加用户', 550, 400);

        }
        //添加部门
        function AddBranch() {
            var node = zTree1.getSelectedNode();
            var pid = node == null ? "1" : node.id;
            var pageurl = "MangerBranch.aspx?Action=add&Pid=" + pid;
            MyDialog.viewform(pageurl, '添加部门', 550, 400);

        }

        $(document).ready(function () {
            getMenu();
            rMenu = $("#smartMenu_image2");
            //鼠标不在菜单中按下时隐藏菜单
            $("body").bind("mousedown",
            function (event) {
                if (!(event.target.id == "smartMenu_image2" || $(event.target).parents("#smartMenu_image2").length > 0)) {
                    $("#smartMenu_image2").hide();
                }
            });
            ;
            var bodyMenuData = [
                [{
                    text: "修改用户",
                    func: function () {
                        var username = $(this).attr('username');
                        var pageurl = "MangerUser.aspx?Action=Update&UserName=" + username;
                        MyDialog.viewform(pageurl, '修改用户', 550, 400);

                    }
                }, {
                    text: "删除用户",
                    func: function () {
                        var username = $(this).attr('username');
                        var tr = $(this).parent().parent();
                        MyDialog.confirm('你确定要删除此用户吗？', function () {

                            $.ajax({
                                type: "GET",
                                url: "AdminHandle.ashx",
                                dataType: "html",
                                data: { "Action": "delUser", "delCode": username },
                                beforeSend: function (XMLHttpRequest) {
                                    MyDialog.Mytips();

                                },
                                success: function (msg) {
                                    if (true) {
                                        tr.remove();
                                    }
                                },
                                complete: function (XMLHttpRequest, textStatus) {
                                    MyDialog.CloseMytips();

                                },
                                error: function () {
                                }
                            });
                        });
                    }
                }]
                //,
                //[{
                //    text: "分配角色",
                //    func: function () {
                //    }
                //}]
            ];
            $("#MulTab tr .UserMenu").smartMenu(bodyMenuData, {
                name: "image"
            });

            $("#conQuery").keyup(function (e) {
                var text = $(this).val();
                if (text != "") {
                    $("#MulTab tbody tr").each(function () {
                        if ($.trim($(this).find('td').eq(1).text()).indexOf(text) >= 0) {
                            $(this).show();
                        }
                        else {
                            $(this).hide();
                        }
                    })
                }
                else {
                    $("#MulTab tbody tr").show();
                }
            });

        })

    </script>
</head>
<body style="margin: 0px">
    <form id="form" runat="server">
        <table id="container">
            <tr>
                <td valign="top">
                    <div id="sidebar" class="well" style="margin: 20px;">
                        <ul id="tree" class="tree">
                        </ul>
                    </div>
                </td>
                <td valign="top" style="width: 90%;">
                    <div class="well" style="margin: 20px;">
                        <div>

                            <table class="table">
                                <tr>
                                    <td style="text-align: left">
                                        <asp:Button ID="btAddBranch" CssClass="btn btn-info" runat="server" Text="添加部门" OnClientClick="AddBranch();return false" />

                                        <asp:Button ID="btAddUser" CssClass="btn btn-info" runat="server" Text="添加用户" OnClientClick="AddUser();return false" />
                                    </td>

                                </tr>
                            </table>
                        </div>
                        <div id="prolist">
                            <asp:Repeater ID="RepRepeaterData" runat="server">
                                <HeaderTemplate>
                                    <table id="MulTab" class="table table-bordered table-condensed table-hover">
                                        <thead>
                                            <tr>

                                                <th>用户名称
                                                </th>
                                                <th>登录账号
                                                </th>
                                                <th>所在部门
                                                </th>
                                                <th>担任职务
                                                </th>
                                                <th>房间号
                                                </th>
                                                <th>是否启用
                                                </th>
                                                <th>详细
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr id="row">
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "UserRealName")%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "UserName")%>
                                        </td>
                                        <td>
                                            <%#GetBranchName(Convert.ToString(DataBinder.Eval(Container.DataItem, "BranchCode")))%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "zhiwu")%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "remark1")%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "IsUse")%>
                                        </td>
                                        <td>
                                            <a class="UserMenu" username='<%#Eval("UserName") %>' href="#">更多操作</a>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </tbody> </table >
                                </FooterTemplate>
                            </asp:Repeater>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        <div id="smartMenu_image2" class="smart_menu_box" style="display: none;">
            <div class="smart_menu_body">
                <ul class="smart_menu_ul">
                    <li class="smart_menu_li" onclick="ManageBranch('add')"><a href="javascript:" class="smart_menu_a">增加部门</a></li>
                    <li class="smart_menu_li" onclick="ManageBranch('update')"><a href="javascript:"
                        class="smart_menu_a">修改部门</a></li>
                    <li class="smart_menu_li" onclick="ManageBranch('del')"><a href="javascript:" class="smart_menu_a">删除部门</a></li>
                    <li class="smart_menu_li_separate">&nbsp;</li>
                    <li class="smart_menu_li" onclick="ManageBranch('addUser')"><a href="javascript:"
                        class="smart_menu_a">添加用户</a></li>
                </ul>
            </div>
        </div>
        <script type="text/javascript" src='../../JqueryPlug/lhgdialog/lhgdialog.js?self=true&skin=idialog'></script>
    </form>
</body>
</html>
